package com.whj.controller;

import com.whj.domain.entity.Menu;
import com.whj.domain.response.ResponseResult;
import com.whj.domain.vo.AddMenuVO;
import com.whj.domain.vo.RoleMenuVO;
import com.whj.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: wanghaijun
 * @Date: 2023/6/13 - 06 - 13 - 9:53 
 * @Description: com.whj.controller
 */

@RestController
@RequestMapping("system")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 针对菜单的状态进行查询。
     *
     * @param status
     * @param menuName
     * @return
     */
    @GetMapping("/menu/list")
    public ResponseResult listByStatus(String status, String menuName) {
        System.out.println(status + menuName);
        return menuService.listByStatus(status, menuName);
    }

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/menu")
    public ResponseResult addMenu(@RequestBody Menu menu) {

        System.out.println(menu.toString());
        return menuService.addMenu(menu);
    }

    @GetMapping("/menu/{id}")
    public ResponseResult<Menu> menuById(@PathVariable("id") Long id){
        return menuService.menuById(id);
    }
    @PutMapping("/menu")
    public ResponseResult updateMenu(@RequestBody Menu menu){
        return menuService.updateMenu(menu);
    }

    @DeleteMapping("/menu/{id}")
    public ResponseResult deleteMenu(@PathVariable("id") Long id){
        return menuService.deleteMenu(id);
    }

    /**
     * 返回子父标签
     * @return
     */
    @GetMapping("/menu/treeselect")
    public ResponseResult treeSelect(){
        List<AddMenuVO> addRoleVOS = menuService.treeSelect();
        return ResponseResult.okResult(addRoleVOS);
    }


    /**
     * 查询角色所关联的菜单
     *
     * @param id
     * @return
     */
    @GetMapping("/menu/roleMenuTreeselect/{id}")
    public ResponseResult selectRoleMenuTree(@PathVariable("id") Long id) {

        System.out.println("id = " + id);

        List<AddMenuVO> menuVOS = menuService.treeSelect();
        List<String> list =menuService.selectMenuIdById(id);
        return ResponseResult.okResult(new RoleMenuVO(menuVOS,list));
    }
}
